package com.helloUIBinder.client.receivers;

import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.helloUIBinder.client.ProjectJsonParser;
import com.helloUIBinder.model.BlogPost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
//import java.util.logging.Logger;

public class BlogPostReceiver {

//    private final static Logger logger = Logger.getLogger(BlogPostReceiver.class.getName());

    private final static String BLOG_POST_WEB_SERVICE_URL = "/rest/blogPostWebService/"; //if you add string, it will be a message to web-service
    private final static String GET_ALL = "all";
    private final static String GET_BLOG_POST_SIZE = "size";
    private final static String BY_ID = "id/";
    private final static String PAGE = "page/";
    private final static String FIELDS = "fields";
    private final static String PUT = "put";
    private final static String DELETE = "delete";


    public static void getBlogPostById(RequestCallback requestCallback, Integer id) {
        getBlogPostWebRequest(requestCallback, BY_ID + String.valueOf(id));
    }

    public static void putBlogPost(RequestCallback requestCallback, BlogPost blogPost) {
        try {
            RequestBuilder blogPostRequestBuilder = new RequestBuilder(RequestBuilder.POST, BLOG_POST_WEB_SERVICE_URL + PUT);
            blogPostRequestBuilder.setRequestData(ProjectJsonParser.getJsonFromEntity(BlogPost.class, blogPost).toString());
            blogPostRequestBuilder.setCallback(requestCallback);
            blogPostRequestBuilder.send();
        } catch (RequestException rEx) {
//            logger.log(Level.WARNING, rEx.getMessage(), rEx);
        }
    }

    public static void removeBlogPost(RequestCallback requestCallback, BlogPost blogPost) {
        try {
            RequestBuilder blogPostRequestBuilder = new RequestBuilder(RequestBuilder.POST, BLOG_POST_WEB_SERVICE_URL + DELETE);
            blogPostRequestBuilder.setRequestData(ProjectJsonParser.getJsonFromEntity(BlogPost.class, blogPost).toString());
            blogPostRequestBuilder.setCallback(requestCallback);
            blogPostRequestBuilder.send();
        } catch (RequestException rEx) {
//            logger.log(Level.WARNING, rEx.getMessage(), rEx);
        }
    }

    public static void getAllBlogPostsSize(RequestCallback requestCallback) {
        getBlogPostWebRequest(requestCallback, GET_BLOG_POST_SIZE);
    }


    public static void findAllBlogPostsByPage(RequestCallback requestCallback, Integer from, Integer pageSize, String sortColumn, String direction) {
        getBlogPostWebRequest(requestCallback, PAGE + String.valueOf(from) + "/" + String.valueOf(pageSize) + "/" + String.valueOf(sortColumn) + "/" + String.valueOf(direction));
    }

    public static void getAllBlogPosts(RequestCallback requestCallback) {
        getBlogPostWebRequest(requestCallback, GET_ALL);
    }

    private static void getBlogPostWebRequest(RequestCallback requestCallback, String additionalString) {
        try {
            RequestBuilder blogPostRequestBuilder = new RequestBuilder(RequestBuilder.GET, BLOG_POST_WEB_SERVICE_URL + additionalString);
            blogPostRequestBuilder.setCallback(requestCallback);
            blogPostRequestBuilder.send();
        } catch (RequestException rEx) {
//            logger.log(Level.WARNING, rEx.getMessage(), rEx);
        }
    }

    private void someMethod() {

        String URL = "http://localhost:8080/rest/blogPostWebService/all";
        String username = "first";
        String password = "first";


        DefaultHttpClient httpClient = new DefaultHttpClient();
        BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
        httpClient.setCredentialsProvider(credentialsProvider);
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
        RestTemplate restTemplate = new RestTemplate(httpComponentsClientHttpRequestFactory);

        String results = restTemplate.getForObject(URL, String.class);
        System.out.println("Response: " + results);

    }
}
